package com.zx.web.dao.impl;

import com.zx.bean.Order;
import com.zx.bean.User;
import com.zx.utils.DataSourceUtils;
import com.zx.web.dao.AdminOrderDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.dao.impl
 * @Description:
 * @Date 2022/9/16 16:15
 */
public class AdminOrderDaoImpl implements AdminOrderDao {
    @Override
    public List<Order> getAllOrder() {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_order";
        try {
            return qr.query(DataSourceUtils.getConnection(),sql,new BeanListHandler<>(Order.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public User findUserById(int uid) {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_user where id =?";
        try {
            return qr.query(DataSourceUtils.getConnection(),sql,new BeanHandler<>(User.class),uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void updateStatus(String oid) {
        QueryRunner qr = new QueryRunner();
        String sql = "UPDATE `tb_order` SET `status` =3 WHERE `id` = ?";
        try {
            qr.update(DataSourceUtils.getConnection(),sql,oid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
